<template>
  <!-- 放置弹层组件 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="handleClose" @open="getEmployeeSimple">
    <!-- 匿名插槽 -->
    <el-form ref="formRef" label-width="120px" :model="form" :rules="formRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqAddDepartment, reqUpdateDepartments } from '@/api/departments'
import { reqGetEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDepts',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    deptsList: {
      type: Array,
      required: true
    }
  },
  data() {
    const checkNameReperat = (rule, value, callback) => {
      let arr = []
      console.log(arr)
      //  this.form.id有值时为编辑状态
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        arr = this.deptsList.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.deptsList.filter(item => item.pid === this.nodeData.id)
      }
      const repeat = arr.some(item => item.name === value)
      repeat ? callback(new Error('部门已存在')) : callback()
    }

    const checkCodeRepeat = (rule, value, callback) => {
      //  this.form.id有值时为编辑状态
      if (this.form.id && this.nodeData.code === value) {
        callback()
        return
      }
      const repeat = this.deptsList.some(item => item.code === value)
      repeat ? callback(new Error('部门编码重复')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      },
      formRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkNameReperat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: ' 请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      // 用于接收axios数据渲染下拉菜单
      peoples: [],
      showTitle: ''
    }
  },
  // computed: {
  //   //  标题 计算属性来改变弹框的标题
  //   showTitle() {
  //     return this.form.id ? '编辑部门' : '新增部门'
  //   }
  // },
  methods: {
    // 1.X按钮
    handleClose() {
      this.$emit('update:showDialog', false) // sync修饰符
      this.$refs.formRef.resetFields() // 清空from表单
      this.form = { // 重置
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      }
    },
    // 2.点击添加部门触发open回调函数来调用axios函数接收数据
    async getEmployeeSimple() {
      const res = await reqGetEmployeeSimple()
      this.peoples = res.data // 渲染下拉列表数据
    },
    // 3.添加部门/更新部门
    clickSubmit() {
      // 兜底校验
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          // 更新部门
          const res = await reqUpdateDepartments(this.form)
          this.$message.success(res.message)
        } else {
          // 新增部门
          const res = await reqAddDepartment({ ...this.form, pid: this.nodeData.id })
          this.$message.success(res.message)
        }
        this.handleClose() // 调用X按钮函数关闭页面清空from表单
        this.$emit('add-success') // 通知父组件重新调用函数渲染页面
      })
    }

    // // 编辑部门
    // async getDepartDetail() {
    //   const res = await reqGetDepartDetail(this.nodeData.id)
    //   // console.log(res)
    //   this.form = res.data
    // }
  }
}
</script>

<style>

</style>
